#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e4 + 5;

int n, m;
int a[N], b[N], num;
void discrete() {
  rep(i, 1, n) b[i] = a[i];
  sort(b + 1, b + n + 1);
  num = unique(b + 1, b + n + 1) - b - 1;
  rep(i, 1, n) a[i] = lower_bound(b + 1, b + num + 1, a[i]) - b;
}
struct Node {
  int mn, mx, tag;
  int ch[2];
} tr[N << 9];
#define mn(p) tr[p].mn
#define mx(p) tr[p].mx
#define tag(p) tr[p].tag
#define lc(p) tr[p].ch[0]
#define rc(p) tr[p].ch[1]
int root[N];
int newnode(int p) {
  static int tot = 0;
  ++tot;
  mn(tot) = mn(p);
  mx(tot) = mx(p);
  tag(tot) = tag(p);
  lc(tot) = lc(p);
  rc(tot) = rc(p);
  return tot;
}
vector<int> pos[N];
void pushup(int p) {
  mn(p) = min(mn(lc(p)), mn(rc(p)));
  mx(p) = max(mx(lc(p)), mx(rc(p)));
}
void build(int& p, int l, int r) {
  p = newnode(p);
  if (l == r) {
    mn(p) = mx(p) = -l;
    return;
  }
  int mid = (l + r) >> 1;
  build(lc(p), l, mid);
  build(rc(p), mid + 1, r);
  pushup(p);
}
void addtag(int d, int& p) {
  p = newnode(p);
  tag(p) += d;
  mn(p) += d;
  mx(p) += d;
}
void pushdown(int p) {
  if (tag(p)) {
    addtag(tag(p), lc(p));
    addtag(tag(p), rc(p));
    tag(p) = 0;
  }
}
void update(int L, int R, int d, int& p, int l, int r) {
  if (L <= l && R >= r) {
    addtag(d, p);
    return;
  }
  pushdown(p);
  p = newnode(p);  // first push down then alter
  int mid = (l + r) >> 1;
  if (L <= mid) update(L, R, d, lc(p), l, mid);
  if (R > mid) update(L, R, d, rc(p), mid + 1, r);
  pushup(p);
}
void build() {
  rep(i, 1, n) pos[a[i]].push_back(i);
  build(root[num + 1], 0, n);
  per(i, num, 1) {
    root[i] = root[i + 1];
    for (int x : pos[i]) update(x, n, 2, root[i], 0, n);
  }
}
int query_mn(int L, int R, int& p, int l, int r) {
  if (L <= l && R >= r) return mn(p);
  pushdown(p);
  int res = 1e9;
  int mid = (l + r) >> 1;
  if (L <= mid) res = min(res, query_mn(L, R, lc(p), l, mid));
  if (R > mid) res = min(res, query_mn(L, R, rc(p), mid + 1, r));
  return res;
}
int query_mx(int L, int R, int& p, int l, int r) {
  if (L <= l && R >= r) return mx(p);
  pushdown(p);
  int res = -1e9;
  int mid = (l + r) >> 1;
  if (L <= mid) res = max(res, query_mx(L, R, lc(p), l, mid));
  if (R > mid) res = max(res, query_mx(L, R, rc(p), mid + 1, r));
  return res;
}
int query(int p1, int p2, int p3, int p4) {
  int l = 1, r = num;
  while (l <= r) {
    int mid = (l + r) >> 1;
    int x = query_mn(p1, p2, root[mid], 0, n);
    int y = query_mx(p3, p4, root[mid], 0, n);
    if (y >= x)
      l = mid + 1;
    else
      r = mid - 1;
  }
  return r;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  discrete();
  build();
  cin >> m;
  int ans = 0;
  while (m--) {
    int p[4];
    rep(i, 0, 3) cin >> p[i];
    rep(i, 0, 3) p[i] = (p[i] + ans) % n + 1;
    sort(p, p + 4);
    ans = b[query(p[0] - 1, p[1] - 1, p[2], p[3])];
    cout << ans << endl;
  }
  return 0;
}